package com.game;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class GameDAO {
    // 获取所有游戏
    public List<Game> getAllGames() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<Game> games = new ArrayList<>();
        try {
            conn = DBHelper.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * FROM games");
            while (rs.next()) {
                Game game = new Game();
                game.setId(rs.getInt("id"));
                game.setName(rs.getString("name"));
                game.setPrice(rs.getDouble("price"));
                games.add(game);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBHelper.close(conn, stmt, rs);
        }
        return games;
    }
}